package uestc.lj.gulishop.product.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

//import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import uestc.lj.gulishop.product.entity.AttrEntity;
import uestc.lj.gulishop.product.entity.AttrGroupEntity;
import uestc.lj.gulishop.product.service.AttrAttrgroupRelationService;
import uestc.lj.gulishop.product.service.AttrGroupService;
import uestc.lj.common.utils.PageUtils;
import uestc.lj.common.utils.R;
import uestc.lj.gulishop.product.service.AttrService;
import uestc.lj.gulishop.product.service.CategoryService;
import uestc.lj.gulishop.product.vo.AttrGroupRelationVO;
import uestc.lj.gulishop.product.vo.AttrGroupWithAttrsVO;


/**
 * 属性分组
 *
 * @author crazlee
 * @email peterlee0@163.com
 * @date 2021-05-21 18:45:10
 */
@RestController
@RequestMapping("product/attrgroup")
public class AttrGroupController {
    @Autowired
    private AttrGroupService attrGroupService;

    @Autowired
    private CategoryService categoryService;

    @Autowired
    private AttrService attrService;

    @Autowired
    private AttrAttrgroupRelationService attrAttrgroupRelationService;


    /**
     * 获取当前分类下的所有属性分组以及其所有属性
     */
    @GetMapping("/{catelogId}/withattr")
    public R getAttrGroupWithAttrs(@PathVariable("catelogId") Long catelogId) {
        // 1.查询出当前分类下的所有属性分组
        // 2.查出每个属性分组的所有属性
        List<AttrGroupWithAttrsVO> attrGroupWithAttrsVOS = attrGroupService.getAttrGroupWithAttrsByCatelogId(catelogId);
        return R.ok().put("data", attrGroupWithAttrsVOS);
    }

    /**
     * 新增属性的关联关系
     *
     * @return
     */
    @PostMapping("/attr/relation")
    public R addRelation(@RequestBody List<AttrGroupRelationVO> attrGroupRelationVOList) {
        attrAttrgroupRelationService.saveBatch(attrGroupRelationVOList);
        return R.ok();
    }

    /**
     * 根据attrid和attrgroupid删除属性与分组的关联关系
     *
     * @return
     */
    @PostMapping("/attr/relation/delete")
    public R deleteAttrRelation(@RequestBody AttrGroupRelationVO[] attrGroupRelationVOS) {
        attrService.deleteRelation(attrGroupRelationVOS);
        return R.ok();
    }

    /**
     * 查询分组没有关联的属性
     */
    @GetMapping("/{attrgroupId}/noattr/relation")
    public R attrNoRelation(@PathVariable("attrgroupId") Long attrgroupId,
                            @RequestParam Map<String, Object> params) {
        PageUtils page = attrService.getNoRelationAttr(params, attrgroupId);
        return R.ok().put("page", page);
    }

    /**
     * 获取当前分组关联的所有属性
     *
     * @param attrgroupId
     * @return
     */
    @GetMapping("/{attrgroupId}/attr/relation")
    public R attrRelation(@PathVariable("attrgroupId") Long attrgroupId) {
        List<AttrEntity> attrEntities = attrService.getRelationAttr(attrgroupId);
        return R.ok().put("data", attrEntities);
    }

    @RequestMapping("/list/{catelogId}")
    public R listById(@RequestParam Map<String, Object> params, @PathVariable("catelogId") Long catelogId) {
        PageUtils page = attrGroupService.queryPage(params, catelogId);
        return R.ok().put("page", page);
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    // @RequiresPermissions("product:attrgroup:list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = attrGroupService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{attrGroupId}")
    // @RequiresPermissions("product:attrgroup:info")
    public R info(@PathVariable("attrGroupId") Long attrGroupId) {
        AttrGroupEntity attrGroup = attrGroupService.getById(attrGroupId);
        // 设置完整路径
        Long catelogId = attrGroup.getCatelogId();
        // 根据当前菜单ID求解出当前菜单的完整路径
        Long[] path = categoryService.findCatelogPath(catelogId);
        attrGroup.setCatelogPath(path);
        return R.ok().put("attrGroup", attrGroup);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //  @RequiresPermissions("product:attrgroup:save")
    public R save(@RequestBody AttrGroupEntity attrGroup) {
        attrGroupService.save(attrGroup);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //  @RequiresPermissions("product:attrgroup:update")
    public R update(@RequestBody AttrGroupEntity attrGroup) {
        attrGroupService.updateById(attrGroup);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    // @RequiresPermissions("product:attrgroup:delete")
    public R delete(@RequestBody Long[] attrGroupIds) {
        attrGroupService.removeByIds(Arrays.asList(attrGroupIds));

        return R.ok();
    }

}
